import {
  getIndexPage,
  getHomeData,
  getShareSetting,
  updateUserTaskComplete
}
from '../../utils/service.js'
var config = require('../../config/config');
var enum_type = require('../../utils/enum');
var util = require('../../utils/util');
var common = require('../../utils/common');

var app = getApp()

Page({
  data: {
    imgUrls: [
      'http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg',
      'http://img06.tooopen.com/images/20160818/tooopen_sy_175833047715.jpg'
    ],
    indicatorDots: false,
    autoplay: true,
    interval: 3000,
    duration: 1000,
  },
  onLoad: function (options) {
    console.log(options);
    this.initData(options);
    //广告主跳转进来
    common.adsClickData(options);
  },
  initData: function (options){
    this.getShareInfo();   
    this.getNavlist();
    this.getAppInfo();
    this.getMainData();
  },
  getAppInfo: function () {

    var that = this;
    var appId = config.appId;
    getIndexPage({
      appid: appId
    }).then(res => {
        wx.setNavigationBarTitle({
          title: res.title//页面标题为路由参数
        })
    }).catch(e => {

    })
  },
  getNavlist:function(){

    let that = this;
    common.getNavlist(function (res) {
      var page = util.getCurrentPageUrl();
      for (var item in res) {

        if (res[item].pageUrl == page) {
          res[item].isSelect = true;
          break;
        }
      }
      that.setData({
        navs: res
      })
    });
  },
  getShareInfo:function(){
    var that = this;
    var appId = config.appId;
    getShareSetting({
      appid: appId,
      "type": enum_type.pageEnum.INDEX
    }).then(res => {
      that.setData({
        shareInfo: res
      })

    }).catch(e => {

    })
  },
  getMainData: function () {
    var that = this;
    var appId = config.appId;
    getHomeData({
      appid: appId
    }).then(res => {
     that.setData({
       imgUrls: res.banners,
      //  items: res.tableLists,
      //  product: res.businesses,
      //  icons: res.navLists,
       classifys: res.classifys,
       hots: res.hotLists
     })
      
    }).catch(e => {

    })
  },
  onShareAppMessage: function (options) {
    
    var shareInfo = this.data.shareInfo;
    if(shareInfo){
      return {
        title: shareInfo.title,
        imageUrl: shareInfo.imageSrc
      }
    }
  },
  jumpPage: function (event) {

    var item = event.currentTarget.dataset.item;
    //用户点击
    if (item.jumpType != 4){
      common.jumpClickData(item.appId);
    }

    common.jumpPage(event);
  },
  onPullDownRefresh() {
    　　
    console.log('--------下拉刷新-------')
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.initData(null);
    wx.hideNavigationBarLoading();
    wx.stopPullDownRefresh();
  }

})